/* 
 * File:   IPC_mmap.h
 * Author: kisme
 *
 * Created on May 11, 2011, 3:45 PM
 */

#ifndef IPC_MMAP_H
#define	IPC_MMAP_H

#include "MMAP_IO.h"
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

/**
 *  config info
 */
template<class NodeT>
struct Mmap_IO_Conf {
    NodeT* m_pPtr;
    // file fd.
    int m_nFileFd;
    //
    int m_nMenLen;
    //
    int m_nNodeLen;
    
    Mmap_IO_Conf();
    
};

template<class NodeT>
class MMAP_IO {
public:
    /**
     * construction.
     */
    MMAP_IO<NodeT>();
    MMAP_IO<NodeT>(const MMAP_IO& orig);

    /**
     * init the file's fd.
     * @param filePath: target file's path.
     * @return status. 0-success, 1-failed.
     */
    int initFd(char* filePath);
    
    /**
     * init point for map.
     * @param len: map space's length.
     * @param offset: offset.
     * @return status. 0-success, 1-ptr init failed, 2-fd hasn't bean init.
     */
    int initPtr(int offset);
    
    /**
     * init space for send data.
     * @param nodelen: struct-data's size.
     * return 0.
     */
    int initSpace(int nodelen);
    
    /**
     * close fd if fd has bean create.
     * @return status.
     */
    int closeFd();
    /**
     * write content into shared-menory.
     * @param content: content to be write.
     * @return status.
     */
    int memwrite(NodeT* struct_content);
    /**
     * sync the disk and memory.
     * @return 
     */
    int memsync();
    /**
     * unmap.
     * @return 
     */
    int menunmap();
    
    /**
     * read data from share-menmory
     */
    int memread(NodeT* struct_content);
    
    int getMenorySize();
    
    int getNodeNum();
    
    virtual ~MMAP_IO<NodeT>();
private:
    Mmap_IO_Conf<NodeT>* mmap_io_conf;
    int fd, menlen, nodenum;
};




#endif	/* IPC_MMAP_H */

